package com.maslyk.invitation.entitybean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.maslyk.invitation.audit.ObjectName;
import com.maslyk.invitation.entitybean.abstrct.GenericEntity;
import com.maslyk.invitation.enums.PermissionEnum;

/**
 *
 * @author kris
 */
@Entity
@Table(name = "salesman")
@ObjectName(label="Sprzedawca")
public class Salesman implements GenericEntity
{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "registrationdate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date registrationdate;
    @Basic(optional = false)
    @Column(name = "login")
    private String login;
    @Column(name = "inactive")
    private boolean inactive = false;
    @Basic(optional = false)
    @Column(name = "password")
    private String password;
    @JoinColumn(name = "personid", referencedColumnName = "id")
    @OneToOne(cascade = { CascadeType.ALL }, optional = false, fetch = FetchType.EAGER)
    private Person personid;
    @Column(name = "permissions")
    private String permissions;
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "operatorId", fetch = FetchType.LAZY)
	private List<Auditlog> logs = new ArrayList<Auditlog>();
    @Transient
    private Map<String,Boolean> permMap;

    public Salesman()
    {
    }

    public Salesman(Integer id)
    {
        this.id = id;
    }

    public Salesman(Integer id, Date registrationdate, String login, String password)
    {
        this.id = id;
        this.registrationdate = registrationdate;
        this.login = login;
        this.password = password;
    }

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public Date getRegistrationdate()
    {
        if(registrationdate == null)
            registrationdate = new Date();
        return registrationdate;
    }

    public void setRegistrationdate(Date registrationdate)
    {
        this.registrationdate = registrationdate;
    }

    public String getLogin()
    {
        return login;
    }

    public void setLogin(String login)
    {
        this.login = login;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public Person getPersonid()
    {
        if(personid == null)
            personid = new Person();
        return personid;
    }

    public void setPersonid(Person personid)
    {
        this.personid = personid;
    }

    public String getPermissions()
    {
        return permissions;
    }

    public void setPermissions(String permissions)
    {
        this.permissions = permissions;
    }
    
    public boolean isInactive() 
    {
		return inactive;
	}

	public void setInactive(boolean inactive) 
	{
		this.inactive = inactive;
	}

	public List<String> getPermissionsList()
    {
        if (permissions == null || permissions.length() == 0)
            return null;

        String[] perms = permissions.split(",");
        
        return Arrays.asList(perms);
    }
    
    public void setPermissionsList(List<String> list){}

    public Map<String,Boolean> getPermissionMap()
    {
        if(permMap != null)
        	return permMap;
    	
    	List<String> salesmanPermission = getPermissionsList();

        if(salesmanPermission == null)
            return null;

        permMap = new HashMap<String,Boolean>();

        for (PermissionEnum permission : PermissionEnum.values())
        {
            if(salesmanPermission.contains(permission.name()))
            	permMap.put(permission.name(), Boolean.TRUE);
            else
            	permMap.put(permission.name(), Boolean.FALSE);
        }
        return permMap;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
    	if(!(object instanceof Salesman))
    		return false;
    	
        Salesman other = (Salesman) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
            return false;
        return true;
    }

    @Override
    public String toString()
    {
        return personid.getSurname() +" "+ personid.getFirstname();
    }

	public List<Auditlog> getLogs() {
		return logs;
	}

	public void setLogs(List<Auditlog> logs) {
		this.logs = logs;
	}
	
	
	public String getKey() {
		return null;
	}
	
	public boolean isInserted() {
		return id != null;
	}
}
